﻿using Lanbt.Core.Entity.Dto;
using Lanbt.Core.Entity.Dto.lanbt_system_config;
using Lanbt.Core.Entity.Dto.quartz;
using Lanbt.Core.Service.Helper;
using Lanbt.Core.Service.Quartz;
using Lanbt.Core.Utility.ExceptionExtent;
using Lanbt.Core.Web.Controllers.Base;
using Microsoft.AspNetCore.Mvc;
using Quartz;

namespace Lanbt.Core.Web.Controllers.Action
{
    /// <summary>
    /// 调度任务
    /// </summary>
    public class QuartzAction : ActionController
    {
        private readonly IQuartzService _quartzBase;
        public QuartzAction(IQuartzService quartzBase)
        {
            _quartzBase = quartzBase;
        }

        /// <summary>
        /// 获取任务详情
        /// </summary>
        /// <param name="name"></param>
        /// <param name="group"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.quartz.job.detail.get")]
        public async Task<ResultOutput<InsertJobInput>> GetJobDetail(string name,string group)
        {
            return await _quartzBase.GetJobDetail(new JobKey(name, group));
        }

        /// <summary>
        /// 获取异常信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.quartz.job.exception.get")]
        public async Task<ResultOutput<string>> GetException(QuartzDtoInput input)
        {
            return await _quartzBase.GetException(new JobKey(input.Name, input.Group));
        }

        /// <summary>
        /// 获取全部任务
        /// </summary>
        /// <returns></returns>
        [HttpGet("/lanbt.quartz.job.quary")]
        public async Task<PagesResultOutput<QueryJobDetailListOutput>> QueryJobDetailList(QueryJobDetailListInput input)
        {
            return await _quartzBase.QueryJobDetailList(input);
        }

        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.quartz.job.loglist.query")]
        public async Task<ResultOutput<string>> QueryLogList(QuartzDtoInput input)
        {
            return await _quartzBase.QueryLogList(new JobKey(input.Name, input.Group));
        }

        /// <summary>
        /// 测试邮箱
        /// </summary>
        /// <param name="mailConfig"></param>
        /// <returns></returns>
        [HttpPost("/lanbt.quartz.config.mail.text")]
        public async Task<ResultOutput> TestEmail([FromBody] MailConfig mailConfig)
        {
            await EmailHelper.TestMailClient(mailConfig);
            return new ResultOutput().ResultInput("连接成功");
        }

        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/lanbt.quartz.job.add")]
        public async Task<ResultOutput> InsertJob([FromBody] InsertJobInput input)
        {
            await _quartzBase.InsertJob(input);
            return new ResultOutput();
        }

        /// <summary>
        /// 恢复任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.quartz.job.resume")]
        public async Task<ResultOutput> ResumeJob([FromBody] QuartzDtoInput input)
        {
            await _quartzBase.ResumeJob(new JobKey(input.Name, input.Group));
            return new ResultOutput();
        }

        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.quartz.job.pause")]
        public async Task<ResultOutput> PauseJob([FromBody] QuartzDtoInput input)
        {
            await _quartzBase.PauseJob(new JobKey(input.Name, input.Group));
            return new ResultOutput();
        }

        /// <summary>
        /// 修改任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("/lanbt.quartz.job.update")]
        public async Task<ResultOutput> UpdateJob([FromBody] UpdateJobInput input)
        {
            
            JobKey oldJobKey = new JobKey(input.OldName, input.OldGroup);
            JobKey jobKey = new JobKey(input.JobDetail.Name, input.JobDetail.Group);
            //验证除了 oldName oldGroup 之外的任务是否重复
            if (await _quartzBase.ExistsJob(oldJobKey, jobKey))
            {
                throw new LanbtException(ExceptionModel.QUARTZ_JOB_REPEAR);
            }
            var runNumber = await _quartzBase.GetRunNumber(oldJobKey);
            await _quartzBase.DeleteJob(oldJobKey);
            await _quartzBase.InsertJob(input.JobDetail,runNumber);
            return new ResultOutput();
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("/lanbt.quartz.job.delete")]
        public async Task<ResultOutput> DeleteJob([FromBody] QuartzDtoInput input)
        {
            await _quartzBase.DeleteJob(new JobKey(input.Name, input.Group));
            return new ResultOutput();
        }
    }
}
